package com.cjx.api.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.cjx.api.common.*;
import com.cjx.api.exception.BusinessException;

import com.cjx.api.model.dto.interfaceinfo.InterfaceInfoAddRequest;
import com.cjx.api.model.dto.interfaceinfo.InterfaceInfoQueryRequest;
import com.cjx.api.model.dto.interfaceinfo.InterfaceInfoUpdateRequest;
import com.cjx.api.model.dto.interfaceinfo.InvokeRequest;
import com.cjx.api.model.entity.InterfaceInfo;
import com.cjx.api.model.vo.InterfaceInfoVO;
import com.cjx.api.service.InterfaceInfoService;
import com.cjx.starsapisdk.client.StarsApiClient;
import com.cjx.starsapisdk.model.response.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户接口调用信息用户接口调用
 *
 * @author cjx
 */
@Tag(name = "用户接口调用信息用户接口调用", description = "用户接口调用相关操作")
@RestController
@RequestMapping("/interfaceInfo")
public class InterfaceInfoController {

    @Resource
    private InterfaceInfoService interfaceInfoService;
    @Resource
    private  StarsApiClient starsApiClient;
    // region 增删改查



    /**
     * 新增用户接口调用信息
     *
     * @param interfaceInfoAddRequest
     * @param request
     * @return
     */
    @Operation(summary = "新增用户接口调用信息", description = "新增用户接口调用信息")
    @PostMapping("/add")
    public BaseResponse<Boolean> addInterfaceInfo(@RequestBody InterfaceInfoAddRequest interfaceInfoAddRequest, HttpServletRequest request) {
        if (interfaceInfoAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceInfo interfaceInfo = BeanUtil.copyProperties(interfaceInfoAddRequest, InterfaceInfo.class);
        //将list的类型的请求参数和返回参数转换成json格式
        interfaceInfo.setRequestParams(JSONUtil.toJsonStr(interfaceInfoAddRequest.getRequestParams()));
        interfaceInfo.setResponseParams(JSONUtil.toJsonStr(interfaceInfoAddRequest.getResponseParams()));
        boolean save = interfaceInfoService.save(interfaceInfo);
        return ResultUtils.success(save);
    }




    /**
     * 删除用户接口调用信息
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @Operation(summary = "删除用户接口调用信息", description = "删除用户接口调用信息")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteInterfaceInfo(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = interfaceInfoService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户接口调用信息
     *
     * @param interfaceInfoUpdateRequest
     * @param request
     * @return
     */
    @Operation(summary = "更新用户接口调用信息", description = "更新用户接口调用信息")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateInterfaceInfo(@RequestBody InterfaceInfoUpdateRequest interfaceInfoUpdateRequest, HttpServletRequest request) {
        if (interfaceInfoUpdateRequest == null || interfaceInfoUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        BeanUtils.copyProperties(interfaceInfoUpdateRequest, interfaceInfo);
        boolean result = interfaceInfoService.updateById(interfaceInfo);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取用户接口调用信息
     *
     * @param id
     * @param request
     * @return
     */
    @Operation(summary = "根据 id 获取用户接口调用信息", description = "根据 id 获取用户接口调用信息")
    @GetMapping("/get")
    public BaseResponse<InterfaceInfoVO> getInterfaceInfoById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceInfo interfaceInfo = interfaceInfoService.getById(id);
        InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
        BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
        return ResultUtils.success(interfaceInfoVO);
    }

    /**
     * 获取用户接口调用信息列表
     *
     * @param interfaceInfoQueryRequest
     * @param request
     * @return
     */
    @Operation(summary = "获取用户接口调用信息列表", description = "获取用户接口调用信息列表")
    @GetMapping("/list")
    public BaseResponse<List<InterfaceInfoVO>> listInterfaceInfo(InterfaceInfoQueryRequest interfaceInfoQueryRequest, HttpServletRequest request) {
        InterfaceInfo interfaceInfoQuery = new InterfaceInfo();
        if (interfaceInfoQueryRequest != null) {
            BeanUtils.copyProperties(interfaceInfoQueryRequest, interfaceInfoQuery);
        }
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>(interfaceInfoQuery);
        List<InterfaceInfo> interfaceInfoList = interfaceInfoService.list(queryWrapper);
        List<InterfaceInfoVO> interfaceInfoVOList = interfaceInfoList.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(interfaceInfoVOList);
    }

    /**
     * 分页获取用户接口调用信息列表
     *
     * @param interfaceInfoQueryRequest
     * @param request
     * @return
     */
    @Operation(summary = "分页获取用户接口调用信息列表", description = "分页获取用户接口调用信息列表")
    @GetMapping("/list/page")
    public BaseResponse<Page<InterfaceInfoVO>> listInterfaceInfoByPage(InterfaceInfoQueryRequest interfaceInfoQueryRequest, HttpServletRequest request) {
        System.out.println("SQL      语句");
        long current = 1;
        long size = 10;
        InterfaceInfo interfaceInfoQuery = new InterfaceInfo();
        if (interfaceInfoQueryRequest != null) {
            BeanUtils.copyProperties(interfaceInfoQueryRequest, interfaceInfoQuery);
            current = interfaceInfoQueryRequest.getCurrent();
            size = interfaceInfoQueryRequest.getPageSize();
        }
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>(interfaceInfoQuery);
        Page<InterfaceInfo> interfaceInfoPage = interfaceInfoService.page(new Page<>(current, size), queryWrapper);
        Page<InterfaceInfoVO> interfaceInfoVOPage = new PageDTO<>(interfaceInfoPage.getCurrent(), interfaceInfoPage.getSize(), interfaceInfoPage.getTotal());
        List<InterfaceInfoVO> interfaceInfoVOList = interfaceInfoPage.getRecords().stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
        interfaceInfoVOPage.setRecords(interfaceInfoVOList);
        return ResultUtils.success(interfaceInfoVOPage);
    }

    // endregion

    /**
     * 调用用户接口调用
     * @param invokeRequest
     * @param request
     * @return
     */
    @PostMapping("/invoke")
    public BaseResponse<Object> invokeInterface(@RequestBody InvokeRequest invokeRequest,HttpServletRequest request){
        if (invokeRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ResultResponse res = interfaceInfoService.invokeInterface(invokeRequest,request);
        return ResultUtils.success(res);
    }

    /**
     * 下线用户接口调用
     * @param idRequest 用户接口调用id
     * @param request
     * @return
     */
    @PostMapping("/offline")
    public BaseResponse<Boolean> offlineInterface(@RequestBody IdRequest idRequest , HttpServletRequest request){
        if (idRequest == null ||  idRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = interfaceInfoService.offlineInterface(idRequest.getId(),request);
        return ResultUtils.success(res);
    }

    /**
     * 上线用户接口调用
     * @param idRequest 用户接口调用id
     * @param request
     * @return
     */
    @PostMapping("/online")
    public BaseResponse<Boolean> onlineInterface(@RequestBody IdRequest idRequest ,HttpServletRequest request){
        if (idRequest == null ||  idRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = interfaceInfoService.onlineInterface(idRequest.getId(),request);
        return ResultUtils.success(res);
    }



}
